package inv.stask.ui;

import inv.stask.beans.Task.Priority;
import inv.stask.logic.Logable;

import java.util.Date;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.MutableComboBoxModel;

public class JErrorComboBox extends JComboBox implements Logable {
	private static final long serialVersionUID = 4860304532935163378L;
	ErrorItem error;
	
	public JErrorComboBox(){
		super(new JDyanmicComboboxModel());
	}

	public void addMessage(String message){
		addItem(message);
		setSelectedItem(message);
	}

}

class JDyanmicComboboxModel extends DefaultComboBoxModel implements MutableComboBoxModel {
	Vector<String> messages	= new Vector<String>();
	private static final long serialVersionUID = -4810540442736464604L;
	
	public Object getElementAt(int index) {
		return messages.elementAt(index);
	}
	
	public int getSize() {
		return messages.size();
	}
	
	@Override
	public void addElement(Object value) {
		messages.add((String)value);
		if(messages.size() > 5)
			messages.remove(0);
	}
	
	@Override
	public void removeElementAt(int index) {
		messages.remove(index);
	}
	
	@Override
	public void removeElement(Object anObject) {
		messages.remove(anObject);
	}
	
	@Override
	public Object getSelectedItem() {
		if(messages.size() == 0)
			return null;
		
		int poz = messages.size() - 1;
		return messages.elementAt(poz);
	}
}

class ErrorItem{
	Priority priority;
	String message;
	Date date;
	
	public static final int ERRORS_SIZE = 10;
	
	private static final LinkedList<ErrorItem> errorList	= new LinkedList<ErrorItem>();
	public void pushError(String errorMessage, Priority priority){
		synchronized (errorList) {
			errorList.push(new ErrorItem(errorMessage, priority));
			if(errorList.size() > ERRORS_SIZE)
				errorList.poll();
		}
	}
	
	private ErrorItem(String message, Priority priority) {
		this.date = new Date();
		this.message = message;
		this.priority = priority;
	}
	
	public Priority getPriority() {
		return priority;
	}
	public void setPriority(Priority priority) {
		this.priority = priority;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
}
